package org.jshand.cloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 作者: 张金山
 * 创建时间：2025/2/11 19:33   星期二
 * 描述： 服务提供者控制器
 * 项目: spring-cloud-alibaba - org.jshand.cloud.controller
 * 作者的博客:  https://blog.fulfill.com.cn
 */
@Slf4j
@RestController
@RequestMapping("/consumer")
public class ConsumerUserController {


    /**
     * 获取当前应用名称
     */
    @Value("${spring.application.name}")
    private String appName;



    @Autowired
    private RestTemplate restTemplate;


    @Autowired
    private DiscoveryClient discoveryClient;


    /**
     * 普通测试方法
     * @return
     */
    @RequestMapping("/user/index")
    String index(){
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        return appName+"@"+time;
    }


    /**
     * 测试 discoveryClient 功能
     * @return
     */
    @RequestMapping("/user/serviceInstance")
    List<ServiceInstance> serviceInstance(){
        log.info("测试使用discoveryClient客户端获取nacos 服务注册中心的 所有 ProviderApp实例");

        String orderServiceName = "ProviderApp";
        // 微服务可是是集群，此处是集合，目前只有一个服务，获取第0个元素
        List<ServiceInstance> instances = discoveryClient.getInstances(orderServiceName);
        return instances;
    }


    /***
     * {
     *         "serviceId": "ProviderApp",
     *         "instanceId": "192.168.232.1#7001#DEFAULT#DEFAULT_GROUP@@ProviderApp",
     *         "host": "192.168.232.1",
     *         "port": 7001,
     *         "secure": false,
     *         "metadata": {
     *             "nacos.instanceId": "192.168.232.1#7001#DEFAULT#DEFAULT_GROUP@@ProviderApp",
     *             "nacos.weight": "1.0",
     *             "nacos.cluster": "DEFAULT",
     *             "nacos.ephemeral": "true",
     *             "nacos.healthy": "true",
     *             "preserved.register.source": "SPRING_CLOUD"
     *         },
     *         "uri": "http://192.168.232.1:7001",
     *         "scheme": null
     *     }
     * @param userId
     * @return
     */
    @RequestMapping("/user/order/{userId}")
    String getProvider(@PathVariable("userId") int userId){


        log.info("基于discoveryClient客户端获取nacos 服务注册中心的 第一个ProviderApp实例");


        String orderServiceName = "ProviderApp";
        // 微服务可是是集群，此处是集合，目前只有一个服务，获取第0个元素
        List<ServiceInstance> instances = discoveryClient.getInstances(orderServiceName);

        // ServiceInstance 代表服务实例
        ServiceInstance serviceInstance = instances.get(0);


        String url = serviceInstance.getUri()+"/provider/order/"+userId;
        String orderInfo = restTemplate.getForObject(url, String.class);
        return "用户微服务(使用discoveryClient)："+orderInfo;
    }


}
